// pages/details-order/details-order.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPayType: false
  },
  //获取数据
  getData() {
    let that = this;
    util.request(api.OrderDetails, {
      orderId: that.data.orderId
    }).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          list: res.data.orderGoods,
          orderInfo: res.data.orderInfo,
          orderSn: res.data.orderInfo.orderSn
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //取消订单
  onCancle(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定取消订单？',
      success(res) {
        if (res.confirm) {
          that.getCancleData();
        }
      }
    })
  },
  //取消订单接口
  getCancleData() {
    let that = this;
    util.request(api.OrderCancel, {
      orderId: that.data.orderId
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        var pages = getCurrentPages(); //当前页面栈
        if (pages.length > 1) {
          var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
          beforePage.changeData(); //触发父页面中的方法
          wx.navigateBack({
            delta: 1
          })
        }
      } else {
        util.myToast(res.errmsg)
      }
    });
  },

  //立即付款
  onPay(e) {
    this.getUserData();
    this.setData({
      showPayType: true,
    })

  },
  //获取用户数据
  getUserData() {
    let that = this;
    util.request(api.Center).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          balance: res.data.user.balance
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //选择余额支付
  onChooseYue(e) {
    this.setData({
      showPayType: false
    })
    this.getBanlenceData();
  },
  //选择微信支付
  onChooseWechat(e) {
    this.setData({
      showPayType: false
    })
    this.onWeChatPay();
  },
  //微信支付
  onWeChatPay: function() {
    let that = this;
    wx.login({
      success: res => {
        var code = res.code;
        util.request(api.StartPay, {
          merReqNo: that.data.orderSn,
          code: code
        }, 'POST').then(function(res) {
          if (res.respCode == 'ZB0000') {
            var payParam = res;
            wx.requestPayment({
              'timeStamp': payParam.timestamp,
              'nonceStr': payParam.noncestr,
              'package': payParam.package,
              'signType': payParam.signtype,
              'paySign': payParam.paysign,
              'success': function(res) {
                //支付成功
                wx.redirectTo({
                  url: '/pages/success-pay/success-pay',
                })

              },
              'fail': function(res) {
                //支付失败

              },
            });
          } else {
            if (util.isEmpy(res.respDesc)) {
              util.myToast(res.errmsg);
              that.getData();
            } else {
              util.myToast(res.respDesc);
            }

          }
        });

      }
    })

  },
  //关闭弹窗
  onClose(e) {
    this.setData({
      showPayType: false
    })
  },
  //余额支付
  getBanlenceData() {
    let that = this;
    util.request(api.BalancePay, {
      merReqNo: that.data.orderSn
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        //支付成功
        wx.redirectTo({
          url: '/pages/success-pay/success-pay',
        })
      } else {

        util.myToast(res.errmsg)
      }
    });
  },
  //确认收货
  onConfirm(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认收货将无法退款!',
      success(res) {
        if (res.confirm) {
          that.getConfirmData();
        }
      }
    })
  },
  //确认收货接口
  getConfirmData() {
    let that = this;
    util.request(api.OrderConfirm, {
      orderId: that.data.orderId
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        var pages = getCurrentPages(); //当前页面栈
        if (pages.length > 1) {
          var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
          beforePage.changeData(); //触发父页面中的方法
          wx.navigateBack({
            delta: 1
          })
        }
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //去评价
  onComment(e) {
    util.toClass('/pages/submit-evaluate/submit-evaluate', {
      orderid: this.data.orderId
    })
  },
  //申请退款
  onRefund(e) {
    util.toClass('/pages/apply-refund/apply-refund', {
      orderid: this.data.orderId
    })
  },
  //删除订单
  onDelete(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除订单？',
      success(res) {
        if (res.confirm) {
          that.getDeleteData();
        }
      }
    })
  },
  //删除订单接口
  getDeleteData() {
    let that = this;
    util.request(api.OrderDelete, {
      orderId: that.data.orderId
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        var pages = getCurrentPages(); //当前页面栈
        if (pages.length > 1) {
          var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
          beforePage.changeData(); //触发父页面中的方法
          wx.navigateBack({
            delta: 1
          })
        }
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //立即使用
  onUse(e) {

    util.toClass('/pages/card_address/card_address', {
      money: this.data.orderInfo.actualPrice,
      orderid: this.data.orderId
    })
  },
  //复制
  onCopy(e) {
    util.mCopy(this.data.orderSn);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var orderId = options.orderid
    this.setData({
      orderId: orderId
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var id = this.data.orderId;
    return {
      title: "一份温暖的礼物",
      path: '/pages/card_receive/card_receive?userid=' + wx.getStorageSync("users").id + '&&orderid=' + id,
      imageUrl: 'https://zebra-1258707023.cos.ap-chengdu.myqcloud.com/yndwd/card_bg.png'
    }
  }
})